package edu.towson.cosc490.incaseofemergency;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class EarthquakeOptions extends Activity {
	Button m_earthquakeKit;
	Button m_earthquakeMap;
	Button m_earthquakeInformation;
	Activity EarthquakeOptionsActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_earthquake_options);
		initLayout();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_earthquake_options, menu);
		return true;
	}
	
	public void initLayout(){
		EarthquakeOptionsActivity = this;
		final LocationManager locationManager =
		        (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		
		m_earthquakeKit = (Button) this.findViewById(R.id.buttonEarthquakeKit);
		m_earthquakeKit.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				Intent openEarthquakeKitIntent = new Intent(EarthquakeOptionsActivity, SupplyKit.class);
				openEarthquakeKitIntent.putExtra("kitType", 4);
				startActivity(openEarthquakeKitIntent);
			}
		});
		
		m_earthquakeMap = (Button) this.findViewById(R.id.buttonEarthquakeMap);
		m_earthquakeMap.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Log.i("debug", "OnClick activated");
				AlertDialog.Builder alert = new AlertDialog.Builder(EarthquakeOptionsActivity);

				alert.setTitle("Map Type");
				
				alert.setPositiveButton("Search for shelter", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						try {
							String address = "red+cross";
							Location myLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
							Intent mapIntent = new Intent(
									android.content.Intent.ACTION_VIEW, Uri
									.parse("geo:" + myLoc.getLatitude() + "," + myLoc.getLongitude() + "?q=" + address));
							startActivity(mapIntent);
							} catch (Exception e) {
							}
					  }
					});

					alert.setNegativeButton("View weather map", new DialogInterface.OnClickListener() {
					  public void onClick(DialogInterface dialog, int whichButton) {
						  Intent weatherMapIntent = new Intent(EarthquakeOptionsActivity, WeatherMap.class);
					startActivity(weatherMapIntent);
					  }
					});

					alert.show();
			}
		});
		
		m_earthquakeInformation = (Button) this.findViewById(R.id.buttonEarthquakeInformation);
		m_earthquakeInformation.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				Intent openEarthquakeInformationIntent = new Intent(EarthquakeOptionsActivity, EarthquakeInformation.class);
				startActivity(openEarthquakeInformationIntent);
			}
		});
	}

}
